package com.ruoyi.company.utils;


import com.ruoyi.common.utils.StringUtils;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.NumberFormat;

/**
 * @author WangChangShuo
 * @date 2022年06月24日 14:38
 */
public class Util {
    public static void main(String[] args) throws IOException {
        copyFolder("E:\\nyterp\\uploadPath", "E:\\uploadPath");
    }

    /**
     * 将金额转换为 大写
     *
     * @param money
     * @return
     */
    public static String upperCases(double money) {
        String[] upNum = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] danwei = {"圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
        //取消科学记数法
        NumberFormat numFormat = NumberFormat.getInstance();
        numFormat.setMaximumFractionDigits(2);//设置小数位个数
        numFormat.setGroupingUsed(false);//取消科学技术发
        String formatNum = numFormat.format(money);
        String strmoney = formatNum + "";//浮点型转为字符型
        String lastUpNum = "null"; //用于存放上个参数的值
        String result = "";//返回的结果

        String substring1 = strmoney.substring(0, 1);
        boolean flag = false;
        if (substring1.equals("-")) {
            substring1 = "负";
            strmoney = strmoney.substring(1, strmoney.length());
            flag = true;
        }

        String[] split = strmoney.split("\\.");
        String strMoney = split[0];
        String point = "";
        //小数部分取值处理。
        if (split.length > 1) {
            point = split[1];
            if (point.length() == 1) {
                point = point.concat("0");
            }
        } else {
            point = "0";
        }
        //大于12位就直接返回。
        int moneyLen = strMoney.length();
        if (money == 0) {
            return "零圆整";
        }
        if (moneyLen > 12) {
            return "金额：" + money + "元，超出大写转换范围。最大金额：999999999999.99元";
        }
        //整数(integer)部分处理。
        if (!"0".equals(strMoney)) {
            for (int i = 0; i < moneyLen; i++) {
                String strNum = strMoney.charAt(i) + "";
                int singleNum = Integer.parseInt(strNum);
                String upSingleNum = upNum[singleNum];
                //上一为不等于0的情况
                if (!"零".equals(lastUpNum)) {
                    if (!"零".equals(upSingleNum)) {
                        result = result.concat(upSingleNum).concat(danwei[moneyLen - i - 1]);
                    } else
                        //为零但是在万、亿位上要加单位 (moneyLen-i)==9 指的是单位：亿。  (moneyLen-i)==5指的是单位：万
                        if ((moneyLen - i) == 5 || (moneyLen - i) == 9) {
                            lastUpNum = "";
                        } else {
                            result = result.concat(upSingleNum);
                        }
                }
                //上一位为0的情况
                if ("零".equals(lastUpNum) && !"零".equals(upSingleNum)) {
                    result = result.concat(upSingleNum).concat(danwei[moneyLen - i - 1]);
                }
                //捕捉上一位数（lastUpNum）为零的情况做优化。
                if ((moneyLen - i) == 5 || (moneyLen - i) == 9) {
                    //排除加单位时前面为"零"的情况。如：两百零万
                    if ("零".equals(lastUpNum) || "null".equals(lastUpNum)) {
                        result = result.substring(0, result.length() - 1);
                    }
                    if (!result.endsWith("亿")) {
                        result = result.concat(danwei[moneyLen - i - 1]);
                    }
                    lastUpNum = "";
                } else {
                    //把当前大写数字复制给 lastUpNum 用于下次判断
                    lastUpNum = upSingleNum;
                }
            }
            //对几万元整和几亿元整(result:五万零或者五亿零零)做优化。
            result = result.replaceAll("零零", "零");
            if (result.endsWith("零")) {
                String substring = result.substring(0, result.length() - 1);
                result = substring;
            }
            result = result.concat("圆");
            result = result.replaceAll("圆圆", "圆");
            result = result.replaceAll("万万", "万");

        }

        //小数(point)部分处理
        if ("0".equals(point)) {
            result = result + "整";
        } else {
            //去 整
//            if(result.endsWith("整")){
//                result = result.substring(0,result.length()-1);
//            }
            if ((point.charAt(0) + "").equals("0")) {
                result = result.concat(upNum[Integer.parseInt(point.charAt(1) + "")] + "分");
            } else if ((point.charAt(1) + "").equals("0")) {
                result = result.concat(upNum[Integer.parseInt(point.charAt(0) + "")] + "角");
            } else {
                result = result.concat(upNum[Integer.parseInt(point.charAt(0) + "")] + "角").concat(upNum[Integer.parseInt(point.charAt(1) + "")] + "分");
            }
        }
        if (flag) {
            result = substring1 + result;
        }
        return result;
    }

    /**
     * 将金额转换为 大写
     *
     * @param money
     * @return
     */
    public static String upperCase(double money) {
        String[] upNum = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[] danwei = {"圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
        //取消科学记数法
        NumberFormat numFormat = NumberFormat.getInstance();
        numFormat.setMaximumFractionDigits(2);//设置小数位个数
        numFormat.setGroupingUsed(false);//取消科学技术发
        String formatNum = numFormat.format(money);
        String strmoney = formatNum + "";//浮点型转为字符型
        String lastUpNum = "null"; //用于存放上个参数的值
        String result = "";//返回的结果
        String[] split = strmoney.split("\\.");
        String strMoney = split[0];
        String point = "";
        //小数部分取值处理。
        if (split.length > 1) {
            point = split[1];
            if (point.length() == 1) {
                point = point.concat("0");
            }
        } else {
            point = "0";
        }
        //大于12位就直接返回。
        int moneyLen = strMoney.length();
        if (money == 0) {
            return "零圆整";
        }
        if (moneyLen > 12) {
            return "金额：" + money + "元，超出大写转换范围。最大金额：999999999999.99元";
        }
        //整数(integer)部分处理。
        if (!"0".equals(strMoney)) {
            for (int i = 0; i < moneyLen; i++) {
                String strNum = strMoney.charAt(i) + "";
                int singleNum = Integer.parseInt(strNum);
                String upSingleNum = upNum[singleNum];
                //上一为不等于0的情况
                if (!"零".equals(lastUpNum)) {
                    if (!"零".equals(upSingleNum)) {
                        result = result.concat(upSingleNum).concat(danwei[moneyLen - i - 1]);
                    } else
                        //为零但是在万、亿位上要加单位 (moneyLen-i)==9 指的是单位：亿。  (moneyLen-i)==5指的是单位：万
                        if ((moneyLen - i) == 5 || (moneyLen - i) == 9) {
                            lastUpNum = "";
                        } else {
                            result = result.concat(upSingleNum);
                        }
                }
                //上一位为0的情况
                if ("零".equals(lastUpNum) && !"零".equals(upSingleNum)) {
                    result = result.concat(upSingleNum).concat(danwei[moneyLen - i - 1]);
                }
                //捕捉上一位数（lastUpNum）为零的情况做优化。
                if ((moneyLen - i) == 5 || (moneyLen - i) == 9) {
                    //排除加单位时前面为"零"的情况。如：两百零万
                    if ("零".equals(lastUpNum) || "null".equals(lastUpNum)) {
                        result = result.substring(0, result.length() - 1);
                    }
                    if (!result.endsWith("亿")) {
                        result = result.concat(danwei[moneyLen - i - 1]);
                    }
                    lastUpNum = "";
                } else {
                    //把当前大写数字复制给 lastUpNum 用于下次判断
                    lastUpNum = upSingleNum;
                }
            }
            //对几万元整和几亿元整(result:五万零或者五亿零零)做优化。
            result = result.replaceAll("零零", "零");
            if (result.endsWith("零")) {
                String substring = result.substring(0, result.length() - 1);
                result = substring;
            }
            result = result.concat("圆");
            result = result.replaceAll("圆圆", "圆");
            result = result.replaceAll("万万", "万");

        }

        //小数(point)部分处理
        if ("0".equals(point)) {
            result = result + "整";
        } else {
            //去 整
//            if(result.endsWith("整")){
//                result = result.substring(0,result.length()-1);
//            }
            if ((point.charAt(0) + "").equals("0")) {
                result = result.concat(upNum[Integer.parseInt(point.charAt(1) + "")] + "分");
            } else if ((point.charAt(1) + "").equals("0")) {
                result = result.concat(upNum[Integer.parseInt(point.charAt(0) + "")] + "角");
            } else {
                result = result.concat(upNum[Integer.parseInt(point.charAt(0) + "")] + "角").concat(upNum[Integer.parseInt(point.charAt(1) + "")] + "分");
            }
        }
        return result;
    }

    /**
     * 字符串按字节数截取
     *
     * @param str
     * @param n
     */
    public static String interception(String[] str, int n) {
        int count = 0;
        String m = "[\u4e00-\u9fa5]";
        String string = "";
        //不是最后一次的话
        for (int i = 0; i < str.length; i++) {
            //str[i + 第几次*n]
            if (str[i].matches(m)) {
                //如果当前字符是汉字，计数器加2
                count += 2;
            } else {
                //如果当前字符不是是汉字，计数器加1
                count += 1;
            }
            //如果当前计数器的值小于n，则直接输出当前字符
            if (count < n) {
                string += str[i];
                //System.out.print(str[i]);
            } else if (count == n) {
                string += str[i];
                //System.out.print(str[i]);
                count = 0;
                //满足n个字节后，就换行
                string += "换行aaaaa";

            } else {//如果当前计数器count的值大于n，说明有汉字，换行输出，且此时计数器count=2
                string += str[i];
                count = 2;
                string += "换行aaaaa";
            }
        }
        return string;
    }

    /**
     * 将字符串转成字符串数组
     *
     * @param string
     * @return
     */
    public static String[] stringToArray(String string) {
        String[] temp = new String[string.length()];
        for (int i = 0; i < temp.length; i++) {
            temp[i] = string.substring(i, i + 1);
        }
        return temp;
    }

    /**
     * 将图片转换成base64格式进行存储
     *
     * @param imagePath
     * @return
     */
    public static String encodeToString(String imagePath) throws IOException {
        String type = StringUtils.substring(imagePath, imagePath.lastIndexOf(".") + 1);
        BufferedImage image = ImageIO.read(new File(imagePath));
        String imageString = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, type, bos);
            byte[] imageBytes = bos.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            imageString = encoder.encode(imageBytes);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageString;
    }

    /**
     * 复制文件夹
     *
     * @param oldPath 旧路径
     * @param newPath 新路径
     */
    public static void copyFolder(String oldPath, String newPath) {
        File oldFile = new File(oldPath);
        //获取该路径下所有的文件夹和文件
        File[] files = oldFile.listFiles();
        //遍历
        for (File file : files) {
            //生成新路径
            String targetPath = newPath + File.separator + file.getName();
            File targetFile = new File(targetPath);

            if (!file.isDirectory()) {
                //旧路径下的是一个文件, 则复制文件
                copyFile(file, targetFile);
            } else {
                //是文件夹,则创建文件夹,再递归
                targetFile.mkdirs();
                //将两个文件夹路径传入
                copyFolder(oldPath + File.separator + file.getName(), newPath + File.separator + file.getName());
            }
        }
    }

    /**
     * 复制文件
     *
     * @param oldPath 旧路径
     * @param newPath 新路径
     */
    public static void copyFile(File oldPath, File newPath) {
        try {
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldPath));
                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newPath))) {
                int len; //读取文件的字节数
                byte[] bys = new byte[1024];
                while ((len = bis.read(bys)) != -1) {
                    bos.write(bys, 0, len);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("复制文件失败：" + e);
        }
    }
}
